import dao.UserDAO;
import entity.User;
import ui.AdminUI;
import ui.StudentUI;
import ui.TeacherUI;

import java.util.Scanner;

public class Main {
    private static final Scanner scanner = new Scanner(System.in);
    public static final UserDAO userDAO = new UserDAO();

    public static void main(String[] args) {
        System.out.println("=== 实验室管理系统 ===");
        User user = login();
        if (user == null) {
            System.out.println("登录失败，程序退出");
            return;
        }
        System.out.println("登录成功！欢迎 " + user.getRealName() + "（" + user.getUsername() + "）");
        showRoleMenu(user);
    }

    // 登录流程
    private static User login() {
        while (true) {
            System.out.print("请输入用户名：");
            String username = scanner.nextLine();
            System.out.print("请输入密码：");
            String password = scanner.nextLine();

            User user = userDAO.login(username, password);
            if (user != null) {
                return user;
            }
            System.out.println("用户名或密码错误，重试？(y/n)");
            String retry = scanner.nextLine();
            if (!retry.equalsIgnoreCase("y")) {
                return null;
            }
        }
    }

    // 角色分发
    private static void showRoleMenu(User user) {
        switch (user.getRole()) {
            case "admin":
                new AdminUI().showMenu();
                break;
            case "teacher":
                new TeacherUI(user).showMenu();
                break;
            case "student":
                new StudentUI(user).showMenu();
                break;
            default:
                System.out.println("未知用户角色");
        }
    }
}
    